#include "OgreApplication.h"

//-------------------------------------------------------------------------------------
OgreApplication::OgreApplication()
 : mRoot(0)
{
}

//-------------------------------------------------------------------------------------
OgreApplication::~OgreApplication()
{
    if (mFrameListener)
        delete mFrameListener;
    if (mRoot)
        delete mRoot;

}

//-------------------------------------------------------------------------------------
void OgreApplication::go()
{
   if (!initialise())
      return;


   mRoot->startRendering();

   // clean up
   destroyScene();
}

//-------------------------------------------------------------------------------------
bool OgreApplication::initialise()
{
   mRoot = new Root();

   // add resource locations
   addResourceLocations();

   // if we cannot initialise Ogre, just abandon the whole deal
   if ( !initOgreCore() ) return false;

  
   createSceneManager();

   createCamera();
   
   createViewports();

   // Set default mipmap level (NB some APIs ignore this)
   TextureManager::getSingleton().setDefaultNumMipmaps(5);

   // Create any resource listeners (for loading screens)
   createResourceListener();

   // Initialise resources
   initResources();

   initTitleScreen();

     
   // Create the scene
   createScene();

   //Create the frame listener
   createFrameListener();

   return true;
};

//-------------------------------------------------------------------------------------
void OgreApplication::createCamera()
{
   // Create the camera
   mCamera = mSceneMgr->createCamera("PlayerCam");

   // Position it at 500 in Z direction
   mCamera->setPosition(Vector3(0,0,0));
   // Look back along -Z
   mCamera->lookAt(Vector3(0,0,0));
   mCamera->setNearClipDistance(5);
}

//-------------------------------------------------------------------------------------
void OgreApplication::addResourceLocations()
{
   // Load resource paths from config file
   ConfigFile cf;
   cf.load("resources.cfg");

   // Go through all sections & settings in the file
   ConfigFile::SectionIterator seci = cf.getSectionIterator();

   String secName, typeName, archName;
   while (seci.hasMoreElements())
   {
      secName = seci.peekNextKey();
      ConfigFile::SettingsMultiMap *settings = seci.getNext();
      ConfigFile::SettingsMultiMap::iterator i;
      for (i = settings->begin(); i != settings->end(); ++i)
      {
         typeName = i->first;
         archName = i->second;
         ResourceGroupManager::getSingleton().addResourceLocation(archName, typeName, secName);
      }
   }
 
}

//-------------------------------------------------------------------------------------
bool OgreApplication::initOgreCore()
{
   // Show the configuration dialog and initialise the system
   // You can skip this and use root.restoreConfig() to load configuration
   // settings if you were sure there are valid ones saved in ogre.cfg
   if(mRoot->restoreConfig() || mRoot->showConfigDialog())
   {
      // If returned true, user clicked OK so initialise
      // Here we choose to let the system create a default rendering window by passing 'true'
      mWindow = mRoot->initialise(true);
      return true;
   }
   else
   {
      return false;
   }

}

//-------------------------------------------------------------------------------------
void OgreApplication::createSceneManager()
{
   // Create the SceneManager, in this case a generic one
   mSceneMgr = mRoot->createSceneManager(ST_EXTERIOR_CLOSE);
}



//-------------------------------------------------------------------------------------
void OgreApplication::createViewports()
{
   // Create one viewport, entire window
   Viewport* vp = mWindow->addViewport(mCamera);
   vp->setBackgroundColour(ColourValue(0,0,0));

   // Alter the camera aspect ratio to match the viewport
   mCamera->setAspectRatio(Real(vp->getActualWidth()) / Real(vp->getActualHeight()));
}

//-------------------------------------------------------------------------------------
void OgreApplication::createResourceListener()
{

}

//-------------------------------------------------------------------------------------
void OgreApplication::initResources()
{
   // Initialise, parse scripts etc
   ResourceGroupManager::getSingleton().initialiseAllResourceGroups();
}

//-------------------------------------------------------------------------------------
void OgreApplication::destroyScene()
{
}

//-------------------------------------------------------------------------------------
void OgreApplication::createFrameListener()
{
        mFrameListener= new ExampleFrameListener(mWindow, mCamera);
        mFrameListener->showDebugOverlay(true);
        mRoot->addFrameListener(mFrameListener);
}
